package customer.zspt.cn.customer.utils;

import android.util.Log;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2018/7/19 0019.
 */
public class WebScoket {
    public  static ExecutorService executorService = Executors.newSingleThreadExecutor();
    public static WebSocketClient webSocketClient;
    public static boolean WebScoketConnected = false;
    //建立連接
    public static void conn(final String token) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                Map<String, String> headers = new HashMap();
                webSocketClient = new WebSocketClient(URI.create("ws://sk.yomiyun.com:9321/?"+token+"&kefu"), new Draft_17(), headers, 10) {
                    @Override
                    public void onOpen(ServerHandshake serverHandshake) {
                       Log.e("fuwu","连接");
                       WebScoketConnected = true;
                    }
                    @Override
                    public void onMessage(String s) {
                        if(HSlistener!=null)
                        {
                            HSlistener.success(s);
                        }
                        if(Clistener!=null)
                        {
                            Clistener.success(s);
                        }
                    }
                    @Override
                    public void onClose(int i, String s, boolean b) {
                        WebScoketConnected = false;
                        System.out.println("client onClose:" + i + " " + s + " " + b);
                        if(Clistener!=null){
                            Clistener.close();
                        }
                    }
                    @Override
                    public void onError(Exception e) {
                        System.out.println("client onError:" + e);
                    }
                };
                webSocketClient.connect();
            }
        });
    }
    public static GetMsgListener Clistener;
    public static void GiveDataListener(GetMsgListener listener)
    {
        Clistener = listener;
    }
    public static HsMsgListener HSlistener;
    public static void HsMsgChangeListener(HsMsgListener listener)
    {
        HSlistener = listener;
    }
    public static RefatorMainMsgCountListener RFlistener;
    public static void RefactorListener(RefatorMainMsgCountListener listener)
    {
        RFlistener = listener;
    }
    public interface  GetMsgListener{
        void success(String data);
        void close();
    }
    public interface  HsMsgListener{
        void success(String data);
    }
    public interface  RefatorMainMsgCountListener{
        void OnRefactor();
    }

}
